package com.example.mntz.service.Impl;

import com.example.mntz.QuartzJob.AccJob;
import com.example.mntz.QuartzJob.TimeLxJob;
import com.example.mntz.dao.MyInvestmentMapper;
import com.example.mntz.dao.TzHttpUrlMapper;
import com.example.mntz.model.*;
import com.example.mntz.service.MyInvestmentService;
import com.example.mntz.util.QuartUtil;
import com.example.mntz.util.TzUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class MyInvestmentServiceImpl implements MyInvestmentService {
    @Autowired
    MyInvestmentMapper dao;
    @Autowired
    private TzHttpUrlMapper tzHttpUrl;
    @Autowired
    QuartUtil quartUtil;

    @Override
    public LayuiTable<Zq> selectNewInvestment(int limit, int page, int id) {
        Page page1 = PageHelper.startPage(page, limit);
        List<Zq> zqs = dao.selectNewInvestment(id);
        LayuiTable<Zq> layuiTable = new LayuiTable<>();
        layuiTable.setData(zqs);
        layuiTable.setCount(page1.getTotal());
        return layuiTable;
    }



    @Override
    public LayuiTable<Zq> selectMyInvestment(Integer limit, Integer page, Zq zq) {
        Page page1 = PageHelper.startPage(page, limit);
        List<Zq> zqs = dao.selectMyInvestment(zq);
        LayuiTable<Zq> layuiTable = new LayuiTable<>();
        layuiTable.setData(zqs);
        layuiTable.setCount(page1.getTotal());
        return layuiTable;
    }
/*查看投资收藏*/
    @Override
    public List SelectCollection(Integer limit, Integer page, TzXk tzXk, int uid) {
        Page page1 = PageHelper.startPage(page, limit);
        tzXk.setFe(uid);
        List<Sctz> sctzs = dao.SelectCollection(tzXk);
        TzXk tzXk1 = null;
        List<TzXk> newSctzs = new ArrayList<>();
        try {
            for (Sctz sctz : sctzs) {
                TzHttpUrl httpUrl = tzHttpUrl.selectTzHttpUrl(sctz.getLb());
                String var = TzUtil.TzUtilGet(sctz.getTzCode(), httpUrl);
                if (sctz.getLb() == 1) {/*股票*/
                    tzXk1 = TzUtil.GpTzXk(var);

                } else if (sctz.getLb() == 2) {/*基金*/
                    tzXk1 = TzUtil.JjTzXk(var);
                }
                tzXk1.setCode(sctz.getTzCode());
                newSctzs.add(tzXk1);
                tzXk1=null;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return newSctzs;
    }

    @Override
    public LayuiTable selectTimelxs(Integer limit, Integer page, Timelx timelx) {
        Page page1 = PageHelper.startPage(page,limit);
        List<Timelx> lx = dao.selectTimelxs(timelx);
        LayuiTable<Timelx> layuiTable = new LayuiTable<>();
        layuiTable.setData( lx);
        layuiTable.setCount(page1.getTotal());
        return layuiTable;
    }
    /*搜索timelx*/
    @Override
    public Timelx selectTimelx(Integer timeLb) {
        return dao.selectTimelx(timeLb);
    }
    @Transactional
    @Override
    public void insertTz(Timelx timelx) {

        try {
        if(timelx.getTimeLb() !=null){
            dao.updataTimeLx(timelx);
            if(timelx.getCron()!=null&& !"".equals(timelx.getCron())){
                quartUtil.updateJob( timelx.getTimeLb().toString(), "lx", timelx.getCron());
            }else {
                quartUtil.deleteJob( timelx.getTimeLb().toString(), "lx");
            }
        }else{
            if(  "z".equals(timelx.getStartTime())){
                dao.insertzq(timelx);
            }
            dao.insertTimeLx(timelx);
            dao.addjie(timelx.getTimeLb());
            if(timelx.getCron()!=null&& !"".equals(timelx.getCron())) {
                    quartUtil.addJob(TimeLxJob.class, timelx.getTimeLb().toString(), "lx", timelx.getCron());
            }
        }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void deletetzsd(Integer timeLb) {
        dao.deletetzsd(timeLb);
    }

    @Override
    public LayuiTable selectzqLists(Integer limit, Integer page, Timelx timelx) {
        Page page1 = PageHelper.startPage(page,limit);
        List<Zq> lx = dao.selectzqLists(timelx);
        LayuiTable<Zq> layuiTable = new LayuiTable<>();
        layuiTable.setData(lx);
        layuiTable.setCount(page1.getTotal());
        return layuiTable;
    }

    @Override
    public Zq selectZq(Integer zqId) {
        return dao.selectZq(zqId);
    }

    @Override
    public void insertZq(Zq zq) {
        if(zq.getZqId() != null ){
            dao.updatezq(zq);
        }else {
            dao.insertZqmx(zq);
        }
    }

    @Override
    public void deletezq(Integer zqId) {
        dao.deletezq(zqId);
    }
    @Transactional
    @Override
    public void adminItimetzmx(Integer timeLb) {
        Timelx timelx = dao.selectTimelx(timeLb);
        dao.insertzq(timelx);
        dao.addjie(Integer.valueOf(timeLb));
    }
}
